<template>
  <div class="system-log-container">
    <div class="spaceForm">
      <el-form :inline="true" :model="queryForm" @submit.native.prevent>
        <el-form-item>
          <el-select
            v-model="queryForm.group_id"
            :clearable="true"
            placeholder="分组"
            @change="queryData"
          >
            <el-option
              v-for="item in group"
              :key="item.id"
              :label="item.group_name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input
            v-model.trim="queryForm.keyword"
            clearable
            placeholder="请输入关键词"
          />
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" type="primary" @click="queryData">
            查询
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table v-loading="listLoading" :data="list">
      <el-table-column label="入住人" min-width="120" prop="customer_name" />
      <el-table-column
        label="联系方式"
        min-width="160"
        prop="customer_mobile"
      />
      <el-table-column
        label="地址"
        min-width="200"
        prop="room_address"
        show-overflow-tooltip
      />
      <el-table-column label="预定周期" min-width="200">
        <template #default="{ row }">
          <div>{{ row.start_date }}至{{ row.end_date }}</div>
        </template>
      </el-table-column>
      <el-table-column label="分组" min-width="100" prop="group_name" />
      <el-table-column align="center" label="操作" width="85">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">详情</el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-image
          class="vab-data-empty"
          :src="require('@/assets/empty_images/data_empty.png')"
        />
      </template>
    </el-table>
    <book-detail ref="bookDetail" @fetch-data="queryData" />
    <el-pagination
      background
      :current-page="queryForm.page"
      :layout="layout"
      :page-size="queryForm.perpage"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
  </div>
</template>

<script>
  import { getBookList } from '@/api/rent'
  import { getgroup } from '@/api/group'
  import bookDetail from '@/views/components/bookDetail'
  export default {
    name: 'CustomerDue',
    components: { bookDetail },
    data() {
      return {
        list: [],
        group: [],
        dictList: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        queryForm: {
          group_id: '',
          keyword: '',
          page: 1,
          perpage: 20,
        },
      }
    },
    created() {
      this.getGroup()
      this.fetchData()
    },
    methods: {
      handleSizeChange(val) {
        this.queryForm.perpage = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.page = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.page = 1
        this.fetchData()
      },
      handleEdit(row) {
        this.$refs['bookDetail'].showEdit(row)
      },
      async fetchData() {
        this.listLoading = true
        var queryData = Object.assign({}, this.queryForm)
        if (queryData.group_id == '') {
          delete queryData.group_id
        }
        if (queryData.keyword == '') {
          delete queryData.keyword
        }
        const { data } = await getBookList(queryData)
        this.list = data.items
        this.total = data.total
        this.listLoading = false
      },
      async getGroup() {
        const { data } = await getgroup({})
        this.group = data
      },
    },
  }
</script>
<style lang="scss" scoped>
  .spaceForm .el-select {
    width: 130px;
  }
</style>
